/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

import java.util.*;
/**
 *
 * @author Do Viet Trung
 */
public class JProcess {
    public static Vector <String> parseString(String text, char pattern) {
        if (text.equals("")) {
            return null;
        }
        
        Vector<String>  res = new Vector<String>();
        String tmp="";
        int index = text.indexOf(pattern);

        while (index >= 0) {
            tmp = text.substring(0, index);
            text = text.substring(index + 1, text.length());
            res.add(tmp);
            index = text.indexOf(pattern);
        }

        if (!text.isEmpty()) {
            res.add(text);
        }

        for(int i = 0; i < res.size(); i++) {
            String tmp2 = res.elementAt(i);
            if(tmp2.indexOf(pattern) >= 0 || tmp2.equals("")) {
                res.removeElementAt(i);
                i--;
            }

            if(tmp2.indexOf('\t') >= 0) {
                tmp2 = trim(tmp2,'\t');
                if(!tmp2.equals("")) {
                    res.setElementAt(tmp2, i);
                }
            }
        }

        return res;
    }

    public static String trim (String text, char pattern) {
        String tmp = "";
        for(int i =0; i <text.length(); i++ ) {
            if(text.charAt(i) != pattern) {
                tmp += text.charAt(i);
            }
        }
        
        return tmp;
    }
    
    public static int findStringIndex(Vector <String> list, char pattern) {
        for(int i = 0; i < list.size(); i++) {
            if(list.elementAt(i).indexOf(pattern) >= 0) {
                return i;
            }
        }

        return -1;
    }

    public static String getStringBetween (String source, char startChar, char endChar) {
        String res = "";
        int idx1 = source.indexOf(startChar);
        int idx2 = source.indexOf(endChar);

        if(idx1 >= 0 && idx2 >= 0) {
            res = source.substring(idx1 + 1, idx2);
        }

        return res;
    }

    public static String subString (String Source, int start, int end) {
        if(Source == null || Source.length() < start || start > end) {
            return null;
        }

        String res = "";
        for(int i = start; i < end; i++) {
            res += Source.charAt(i);
        }

        return res;
    }
    
    public static Vector<String> removeCharBegin (Vector <String> list, int idx) {
        for(int i = 0; i < list.size(); i++) {
            String tmp = list.elementAt(i);
            list.setElementAt(tmp.substring(idx + 1), i);
        }

        return list;
    }


    public static String reverseString (String str) {
        StringBuffer sb = new StringBuffer (str).reverse();
        return sb.toString();
    }

//    public static boolean compareString(String Source, String pattern)
//    {
//        if(Source.length() < pattern.length()) return false;
//        Source.
//    }

}